package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.service.DeviceDataService;
import com.zzyl.vo.DeviceDataVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Service
public class DeviceDataServiceImpl implements DeviceDataService {

    @Resource
    private DeviceDataMapper deviceDataMapper;

    /**
     * 条件分页查询设备数据
     *
     * @param dto
     * @return
     */
    @Override
    public PageResponse<DeviceDataVo> getDeviceDataPage(DeviceDataPageReqDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        // 前端传递的时间是一个long类型的毫秒值，作为条件查询数据库需要转换为日期类型
        dto.setStartDate(dto.getStartTime() == null ? null : LocalDateTimeUtil.of(dto.getStartTime()));
        dto.setEndDate(dto.getEndTime() == null ? null : LocalDateTimeUtil.of(dto.getEndTime()));
        // 查询
        Page<DeviceDataVo> page = deviceDataMapper.selectByPage(dto);
        return PageResponse.of(page, DeviceDataVo.class);
    }

    @Override
    public void deleteDeviceData(LocalDateTime localDateTime) {
        deviceDataMapper.deleteByTime(localDateTime);
    }
}
